package cn.xspace.wnx.activity;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.xspace.android.xafcommon.base.BaseActivity;
import com.xspace.android.xafcommon.been.BaseReturnBean;
import com.xspace.android.xafcommon.util.Constants;
import cn.xspace.library.util.MyInfoUtil;
import cn.xspace.library.util.OkHttpManager;
import com.xspace.android.xafcommon.util.RequestConstant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.xspace.app.view.TitleBar;
import cn.xspace.wnx.R;
import cn.xspace.wnx.adapter.WnxOrgListAdapter;
import cn.xspace.wnx.entity.WNXBean;
import cn.xspace.wnx.util.Navigator;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by liuchen on 2016/11/15.
 */


/**
 * 给我推送过来的微农信机构列表页面
 */
public class WnxorgListActivity extends BaseActivity {

    private TitleBar mTitlebar;
    private ListView mListview;
    private String user_id = "";
    private WnxOrgListAdapter mAdapter;
    private ArrayList<WNXBean> dataList;

    @Override
    protected void getIntentData() {
        super.getIntentData();
    }

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_wnx_org_list);
    }

    @Override
    protected void initFindViewById() {
        super.initFindViewById();
        mTitlebar = (TitleBar) findViewById(R.id.title_bar);
        mListview = (ListView) findViewById(R.id.listview_org);
        dataList = new ArrayList<>();
        mAdapter = new WnxOrgListAdapter(mContext, dataList);
        mListview.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        user_id = MyInfoUtil.getMyUserid(mContext);
        requestData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mTitlebar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int index = position - mListview.getHeaderViewsCount();
                WNXBean bean = dataList.get(index);
                //查询自己的微农信消息
//                Navigator.startOrgInfoActivity(mContext, dataList.get(index).msg_sender_id);
                bean.unreadCount = 0;
                Navigator.startWnxListActivity(mContext, bean);
            }
        });
    }

    private void requestData() {
        showWaitDialog();
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put(RequestConstant.KEY_USER_ID, user_id);
//        dataMap.put(RequestConstant.KEY_PG, mPagebean);
        registerSubscription(OkHttpManager.getInstance().commonRequest(Constants.QUERY_MICRO_AGRORG, dataMap)
                .subscribeOn(Schedulers.io())
                .map(new Func1<BaseReturnBean, ArrayList<WNXBean>>() {
                    @Override
                    public ArrayList<WNXBean> call(BaseReturnBean baseReturnBean) {
                        ArrayList<WNXBean> beanList = new ArrayList<WNXBean>();
                        try {
                            JSONArray jsonArray = new JSONArray(baseReturnBean.data);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObj = jsonArray.optJSONObject(i);
                                WNXBean bean = new WNXBean();
                                bean.msg_sender_id = jsonObj.optString("dept_id");
                                bean.msg_sender_name = jsonObj.optString("dept_name");
                                bean.msg_logo = jsonObj.optString("dept_logo");
                                beanList.add(bean);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        return beanList;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe( new Subscriber<ArrayList<WNXBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        hideWaitDialog();
                    }

                    @Override
                    public void onNext(ArrayList<WNXBean> wnxBeen) {
                        hideWaitDialog();
                        dataList.addAll(wnxBeen);
                        mAdapter.notifyDataSetChanged();
                    }
                }));
    }
}
